<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{item.name}}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button @click="hSubmit" type="primary">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserByIdDetail } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props:{
    curId:{
      type:String,
      required:true
    },
  },
  data() {
    return {
      roleIds: [],
      list:[]
    }
  },
  created(){
    this.loadRoles()
  },
  methods: {
    //保存员工分配的角色
    async hSubmit(){
      try {
        await assignRoles({id:this.curId,roleIds:this.roleIds})
        // console.log('assignRoles', res)
        this.$emit('close')
      } catch (err) {
        console.log('assignRoles', err)
      }
    },
    async loadRoles(){
      try {
        const res = await getRoles({page:1,pagesize:100})
        // console.log('getRoles', res)
        const detail =await getUserByIdDetail(this.curId)
        // console.log(detail,'detail');
        this.roleIds=detail.data.roleIds
        this.list=res.data.rows
      } catch (err) {
        console.log('getRoles', err)
      }
    },
  }
}
</script>
